import '../../../app_files.dart';

class ConfigValue {
  final String key;
  final String name;

  const ConfigValue({
    this.key,
    this.name,
  });

  static fromJson(Map<String, dynamic> json) {
    return ConfigValue(
      key: CommonUtils.checkNullToString(json['Key']),
      name: CommonUtils.checkNullToString(json['Value']),
    );
  }

  @override
  bool operator ==(other) {
    if (other.runtimeType != runtimeType) return false;
    final ConfigValue typedOther = other;
    return typedOther.key == key && typedOther.name == name;
  }

  @override
  int get hashCode => name.hashCode ^ key.hashCode;
}

class OrderConfigModel {
  final String configID;
  final String configName;
  final List<ConfigValue> configs;
  final List<ConfigValue> values;
  bool isNormal;
  bool hasOther;
  TextEditingController textEditingController = TextEditingController();
  final List imageList;
  final String valueDesc;

  String get valueStr {
    if (valueDesc != null) return valueDesc;
    if (this.isNormal) return '正常';
    List<String> values = this.values.map((e) => e.name).toList();
    if (this.hasOther) values.add(this.textEditingController.text);
    return values.join('、');
  }

  OrderConfigModel({
    this.configID,
    this.configName,
    this.configs,
    this.values,
    this.imageList,
    this.hasOther,
    this.valueDesc,
  });

  static fromConfigJson(Map<String, dynamic> json) {
    List configValue = json['ConfigValue'];
    List<ConfigValue> configs =
    configValue.map<ConfigValue>((e) => ConfigValue.fromJson(e)).toList();
    return OrderConfigModel(
      configID: CommonUtils.checkNullToString(json['ConfigId']),
      configName: CommonUtils.checkNullToString(json['ConfigKey']),
      configs: configs,
      values: List<ConfigValue>(),
      imageList: List(),
      hasOther: false,
    );
  }

  static fromJson(json) {
    return OrderConfigModel(
      configID: CommonUtils.checkNullToString(json['ConfigId']),
      configName: CommonUtils.checkNullToString(json['ConfigKey']),
      imageList: json['ImageList'],
      valueDesc: CommonUtils.checkNullToString(json['ConfigValue']),
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['ConfigId'] = this.configID;
    data['ConfigKey'] = this.configName;

    if (this.isNormal)
      data['ConfigValue'] = '正常';
    else {
      data['ImageList'] = this.imageList;
      data['ConfigValue'] = this.valueStr;
    }
    return data;
  }
}
